package com.shouxue.pickrvdemo;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

public class PickDialog extends Dialog {
    public PickDialog(@NonNull Context context) {
        this(context,0);
    }

    public PickDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    private String selectTxt;
    private List<String> list;
    private void init() {
        setContentView(R.layout.dialog_language);
        setCanceledOnTouchOutside(false);
        WheelPicker wpLanguage = findViewById(R.id.wp_language);
        list = new ArrayList<>();
        for (int i=0;i<10;i++){
            list.add("语言"+i);
        }
        wpLanguage.setData(list);
        wpLanguage.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                if (list.size() > 0 && position < list.size()) {
                    selectTxt = list.get(position);
                }
            }
        });
        wpLanguage.setBlockClickEvents(true);

        findViewById(R.id.btn_dialog_confirm).setOnClickListener(v -> {
            int position = wpLanguage.stopScrollReturnPosition();
            selectTxt = list.get(position);
            Toast.makeText(getContext(), selectTxt, Toast.LENGTH_SHORT).show();
            dismiss();
        });
        findViewById(R.id.btn_dialog_cancel).setOnClickListener(v -> dismiss());

    }

//    private void refreshCurLanguage() {
//        if (list.size() > 0) {
//            for (int i = 0; i < list.size(); i++) {
//                String languageInfo = list.get(i);
//                if (TextUtils.equals(languageInfo, mCurrentLanguage)) {
//                    wpLanguage.setSelectedItemPosition(i, false);
//                }
//            }
//        }
//    }
}
